package com.wx.mall.goods.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.goods.dto.AppaiseDto;
import com.wx.mall.goods.dto.AppaiseUpdateAllDto;
import com.wx.mall.goods.entity.Appaise;
import com.wx.mall.goods.service.IAppaiseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author j
 * @since 2020-05-04
 */
@RestController
@RequestMapping("/appaise")
@Api(tags = "评价表",description = "AppaiseController")
@Slf4j
public class AppaiseController {

    @Resource
    private IAppaiseService service;

    @ApiOperation("获取数据列表（带筛选和排序）")
    @GetMapping("/getList")
    public CommonResult<CommonPage<Appaise>> getList(@ApiParam("获取dto数据")AppaiseDto dto){
        System.out.println(dto);
        if (dto.getPageNum() == null || dto.getPageSize() == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (dto.getPageNum() <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }

List<Appaise> list=service.getList(dto);

        return CommonResult.success(CommonPage.restPage(list));


    };

    @ApiOperation("根据商品id查询并分页")
    @GetMapping("/getListBySpuId")
    public CommonResult<CommonPage<Appaise>> getListBySpuId(Integer pageNum,Integer pageSize,@ApiParam("商品id")Integer spuId){
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }


        List<Appaise> list=service.getListByUseId(pageNum,pageSize,spuId);

        return CommonResult.success(CommonPage.restPage(list));

    }

    @ApiOperation("批量操作(显示隐藏和删除)")
    @PostMapping("/updateAll")
    public CommonResult updateAll(AppaiseUpdateAllDto dto){


        int sucess=service.updateAll(dto.getIds(),dto.getType());

        return sucess==1?CommonResult.success():CommonResult.failed();

    };

    @ApiOperation("单条删除")
    @DeleteMapping("/delete/{id}")
    public CommonResult deleteOne( @ApiParam("评价表id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id){

        boolean suceess= service.removeById(id);

        return suceess?CommonResult.success():CommonResult.failed();

    };

    @ApiOperation("修改数据")
    @PutMapping("/updateOne")
    public CommonResult updateOne(Appaise appaise){

        boolean sucess=service.updateById(appaise);
        return sucess?CommonResult.success():CommonResult.failed();
    }




}

